<?php

class Api {
	const GET = "GET";
	const POST = "POST";

	private $aryRoutes;

	function __construct() {
		$this->aryRoutes = array();
	}

	function register($route) {
		array_push($this->aryRoutes, $route);
	}

	function execute($method, $path) {
		$executed = false;
		foreach($this->aryRoutes as $objRoute) {
			if($objRoute->matches($method, $path)){
				$objRoute->execute($this);
				$executed  = true;
				break;
			}
		}

		if(!$executed){
			$this->send(array("msg" => "Not found"), 404);
		}
	}

	function getHttpStatusMsg($status) {
		if($status == "200") return "Ok";
		else if($status == "404") return "Not found";
		else if($status == "401") return "Not authorized";
		else if($status == "500") return "Server Error";
		else return "FooBar!";
	}

	function send($data, $status = 200) {
		header("HTTP/1.1 " . $status . $this->getHttpStatusMsg($status));
		header("Content-Type: application/json");
		
		echo json_encode(array("status" => ($status == 200 ? "ok" : "error"), "data" => $data));
	}
};
?>